package com.ikangtai.shecare.home.circlecalendar;

import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;

import com.ikangtai.shecare.R;
import com.ikangtai.shecare.common.App;

/**
 * Created by leo on 16/5/7.
 */
public class ZoomOutPageTransformer implements ViewPager.PageTransformer {

    private static final float MIN_SCALE = 0.9f;
    private static float defaultScale = 0.9f;
    private float defaultRotation = 1000f;
    private float flipCycle = 0.195f;
    private int density = App.getPreferenceInt("density");
    private int otherH;
    private int screenHeight;
    private boolean isfront = true;

    public void transformPage(View view, float position) {

        int pageWidth = view.getWidth();
        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            view.setScaleX(defaultScale);
            view.setScaleY(defaultScale);
        } else if (position <= 1) {
            int paddingTop = (int) (300 * Math.abs(position));
            if (position < flipCycle && position > -flipCycle) {

                float rotation = defaultRotation * position;
                float scale = 1 - Math.abs(position * 4);
                view.findViewById(R.id.percent_circle_progress_bar).setScaleX(1);
                view.findViewById(R.id.it_percent_linearLayout).setScaleX(1);
                view.findViewById(R.id.day_circle_progress_bar).setScaleX(1);
                view.findViewById(R.id.it_day_linearLayout).setScaleX(1);

                view.findViewById(R.id.percent_circle_progress_bar).setScaleY(1);
                view.findViewById(R.id.it_percent_linearLayout).setScaleY(1);
                view.findViewById(R.id.day_circle_progress_bar).setScaleY(1);
                view.findViewById(R.id.it_day_linearLayout).setScaleY(1);


                view.findViewById(R.id.percent_circle_progress_bar).setVisibility(View.VISIBLE);
                view.findViewById(R.id.it_percent_number).setVisibility(View.VISIBLE);
                view.findViewById(R.id.it_percent_title).setVisibility(View.VISIBLE);
                view.findViewById(R.id.it_percent_state).setVisibility(View.VISIBLE);

                view.findViewById(R.id.day_circle_progress_bar).setVisibility(View.VISIBLE);
                view.findViewById(R.id.it_day_number).setVisibility(View.VISIBLE);
                view.findViewById(R.id.it_day_title).setVisibility(View.VISIBLE);
                view.findViewById(R.id.it_day_state).setVisibility(View.VISIBLE);
                view.findViewById(R.id.it_day_word).setVisibility(View.VISIBLE);
                view.setTranslationY((float) (-paddingTop * (density * 0.85) + getTranslationYNum(view) + 60));

                view.findViewById(R.id.it_day_day).setVisibility(View.GONE);
                view.findViewById(R.id.it_day_percent).setVisibility(View.GONE);

                view.setScaleX((float) (scale * 1.6));
                view.setScaleY((float) (scale * 1.6));
                view.setRotationY(rotation);

            } else {
                // [-1,1]
                // Modify the default slide transition to shrink the page as well

                view.findViewById(R.id.it_day_day).setVisibility(View.VISIBLE);
                view.findViewById(R.id.it_day_percent).setVisibility(View.VISIBLE);
                view.setRotationY(0);//不翻转
                view.setScaleX(1);
                view.setScaleY(1);
                float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
                float horzMargin = pageWidth * (1 - scaleFactor) / 2;
                view.setTranslationY((float) ((getTranslationYNum(view) * density / (density - 0.65)) * 1.15 - (getTranslationYNum(view) * Math.abs(position) * (3 - Math.abs(position)))));


                view.findViewById(R.id.percent_circle_progress_bar).setScaleX(flipCycle);
                view.findViewById(R.id.it_percent_linearLayout).setScaleX(flipCycle);
                view.findViewById(R.id.day_circle_progress_bar).setScaleX(flipCycle);
                view.findViewById(R.id.it_day_linearLayout).setScaleX(flipCycle);

                view.findViewById(R.id.percent_circle_progress_bar).setScaleY(flipCycle);
                view.findViewById(R.id.it_percent_linearLayout).setScaleY(flipCycle);
                view.findViewById(R.id.day_circle_progress_bar).setScaleY(flipCycle);
                view.findViewById(R.id.it_day_linearLayout).setScaleY(flipCycle);

                view.findViewById(R.id.it_day_day).setScaleX(1);
                view.findViewById(R.id.it_day_day).setScaleY(1);
                view.findViewById(R.id.it_day_percent).setScaleX(1);
                view.findViewById(R.id.it_day_percent).setScaleY(1);

                view.findViewById(R.id.percent_circle_progress_bar).setVisibility(View.INVISIBLE);
                view.findViewById(R.id.it_percent_number).setVisibility(View.INVISIBLE);
                view.findViewById(R.id.it_percent_title).setVisibility(View.INVISIBLE);
                view.findViewById(R.id.it_percent_state).setVisibility(View.INVISIBLE);

                view.findViewById(R.id.day_circle_progress_bar).setVisibility(View.INVISIBLE);
                view.findViewById(R.id.it_day_number).setVisibility(View.INVISIBLE);
                view.findViewById(R.id.it_day_title).setVisibility(View.INVISIBLE);
                view.findViewById(R.id.it_day_state).setVisibility(View.INVISIBLE);
                view.findViewById(R.id.it_day_word).setVisibility(View.INVISIBLE);

                view.findViewById(R.id.it_day_percent).setVisibility(View.VISIBLE);
                view.findViewById(R.id.it_day_day).setVisibility(View.VISIBLE);

                // Scale the page down (between MIN_SCALE and 1)
            }
        } else { // (1,+Infinity]
            // This page is way off-screen to the right.
            view.setScaleX(defaultScale);
            view.setScaleY(defaultScale);
        }


    }

    private float getTranslationYNum(View view) {
        DisplayMetrics dm = view.getResources().getDisplayMetrics();
        dm = view.getResources().getDisplayMetrics();

        screenHeight = dm.heightPixels;     // 屏幕高（像素，如：800px）

        return (float) (-60 + 0.1094 * screenHeight);
    }


}
